import {NextRequest, NextResponse} from "next/server";
import {prisma} from "@/lib/db";

export async function POST(req: NextRequest) {
    const body = await req.json()
    const name = body.name
    const studentNumber = body.studentNumber
    const stuClass = body.class
    const enrollmentYear = body.enrollmentYear

    if (!name || !studentNumber || !stuClass || !enrollmentYear)
        return NextResponse.json({"msg": "error!"}, {
            status: 500
        })

    const formattedDate = new Date(enrollmentYear).toISOString();
    console.log(formattedDate)
    try {
        const exist_resume = await prisma.student.findFirst({
            where: {
                AND: [
                    {name},
                    {studentNumber},
                    {class: stuClass},
                    {enrollmentYear: formattedDate}
                    // {stuName: stuName}
                ]
            }
        })
        if (exist_resume) return NextResponse.json({"msg": "数据已存在!"}, {
            status: 500
        })

        const student = await prisma.student.create({
            data: {
                // name, stuName
                name, studentNumber, enrollmentYear: formattedDate, class: stuClass
            }
        })
        console.log(student)
        return NextResponse.json({student})
    } catch (err) {
        console.log(err)
        return NextResponse.json({"msg": "error!"}, {
            status: 500
        })
    }

}